//
//  FANRefreshControl.m
//  Weibo - OC
//
//  Created by user on 16/4/24.
//  Copyright © 2016年 user. All rights reserved.
//

#import "FANRefreshControl.h"

//#warning 宏定义一定要放到 导入头文件的 上面, 会影响编译

//define this constant if you want to use Masonry without the 'mas_' prefix

#define MAS_SHORTHAND

//define this constant if you want to enable auto-boxing for default syntax

#define MAS_SHORTHAND_GLOBALS

#import <Masonry/Masonry.h>

#define FANRefreshHeight 50



@interface FANRefreshControl ()

@property (nonatomic, strong) UIImageView *upDownImageView;

@property (nonatomic, strong) UILabel *label;

@property (nonatomic, strong) UIActivityIndicatorView *indicatorView;

@property (nonatomic, strong) UITableView *tableView;



@end

@implementation FANRefreshControl

- (void)setFanState:(FANRefreshState )fanState{
    
    _fanState = fanState;
        
    switch (fanState) {
        case Normal:
        {
            self.label.text = @"下拉刷新";
            
            self.upDownImageView.hidden = false;
            
            [self.indicatorView stopAnimating];
            
            [UIView animateWithDuration:0.25 animations:^{
                
                self.upDownImageView.transform = CGAffineTransformIdentity;
                
            }];
            
           
            
        }
            break;
            
        case Pulling:
        {
            
            self.label.text = @"松手刷新";
            
            self.indicatorView.hidden = true;
            
            [UIView animateWithDuration:0.25 animations:^{
               
                self.upDownImageView.transform = CGAffineTransformMakeRotation(M_PI);
                
            }];
            
          
            
        }
            break;
            
        case Refreshing:
        {
            
            self.upDownImageView.hidden = true;
            
            self.label.text = @"正在刷新";
            
            [self.indicatorView startAnimating];
            
            [self sendActionsForControlEvents:UIControlEventValueChanged];
            
            [UIView animateWithDuration:0.25 animations:^{
               
                UIEdgeInsets edgeInset = self.tableView.contentInset;
                
                edgeInset.top = edgeInset.top + FANRefreshHeight;
                
                self.tableView.contentInset = edgeInset;
                
            }];

        }
            break;
            
        default:
            break;
    }
}

- (UIActivityIndicatorView *)indicatorView{
    
    if (!_indicatorView) {
        
        _indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    }
    
    return _indicatorView;
}

- (UIImageView *)upDownImageView{
    
    if (!_upDownImageView) {
        
        _upDownImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tableview_pull_refresh"]];
    }
    return _upDownImageView;
}

- (UILabel *)label{
    
    if (!_label) {
        
        _label = [[UILabel alloc] init];
        
        _label.text = @"下拉刷新";
        
        _label.font = [UIFont systemFontOfSize:12];
        
        _label.textColor = [UIColor darkGrayColor];
    }
    
    return _label;
}

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
     
        [self setupUI];
        
    }
    
    return self;
}

- (void)willMoveToSuperview:(UIView *)newSuperview{
    
    [super willMoveToSuperview:newSuperview];
    
    self.tableView = (UITableView *)newSuperview;
    
    self.frame = CGRectMake(0, -FANRefreshHeight, self.tableView.frame.size.width, FANRefreshHeight);
    
    [self.tableView addObserver:self forKeyPath:@"contentOffset" options:0 context:nil];
    
}

- (void)endRefreshing{
    
    if (self.fanState == Refreshing) {
        
        self.fanState = Normal;
        
        [UIView animateWithDuration:0.25 animations:^{
            
            UIEdgeInsets edgeInset = self.tableView.contentInset;
            
            edgeInset.top = edgeInset.top - FANRefreshHeight;
            
            self.tableView.contentInset = edgeInset;
            
        }];
        
    }
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    
    CGFloat contentOffsetY = self.tableView.contentOffset.y;
    
    CGFloat contentValue = -(CGFloat)self.tableView.contentInset.top - FANRefreshHeight;
    
    //拽住
    if (self.tableView.dragging) {
        
        if (contentOffsetY < contentValue && self.fanState == Normal)  {
                        
            self.fanState = Pulling;
            
        }else{
            
            if (contentOffsetY >= contentValue && self.fanState == Pulling ) {
                
                self.fanState = Normal;
            }
            
            
        }
        
    }else{//松手
        
        
        if (self.fanState == Pulling){
            
//            NSLog(@"开始刷新了!!!");
            
            self.fanState = Refreshing;
        }
        
    }
    
}

- (void)setupUI{
    
    self.backgroundColor = [UIColor orangeColor];
    
    [self addSubview:self.upDownImageView];
    [self addSubview:self.label];
    [self addSubview:self.indicatorView];
    
    [self.upDownImageView makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.centerX).offset(-35);
        
        make.centerY.equalTo(self.centerY);
        
    }];
    
    [self.label makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.centerY);
        
        make.leading.equalTo(self.upDownImageView.trailing);
    }];
    
    [self.indicatorView makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.centerX).offset(-35);
        
        make.centerY.equalTo(self.centerY);
    }];
}











@end
